//
//  GongGaoDetailViewController.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/4/10.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "GongGaoDetailViewController.h"
#import "EditGongGaoViewController.h"

@interface GongGaoDetailViewController ()

@property (nonatomic, strong) UILabel *gongGaoTitleLabel; // 公告标题
@property (nonatomic, strong) UILabel *senderNameLabel; // 发送者姓名
@property (nonatomic, strong) UILabel *sendTimeLabel; // 发送时间
@property (nonatomic, strong) UILabel *contentLabel; // 公告内容
@property (nonatomic, strong) UIView *bottomWhiteView; // 底部编辑公告背景
@property (nonatomic, strong) UIButton *editGongGaoButton; // 编辑公告

@end

@implementation GongGaoDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // navi的修改
    [self changeNavigation];
    // 建立数据源
    [self addData];
    // 布局
    [self addMySubview];
}

// navi的修改
- (void)changeNavigation
{
    [self.navigationController.navigationBar setTranslucent:NO];
    self.navigationItem.title = @"公告详情";
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
    UITapGestureRecognizer *leftTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
    [leftImageView addGestureRecognizer:leftTapGes];
    
    UIImageView *rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    rightImageView.image = [UIImage imageNamed:@"delete-myGroup"];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightImageView];
    // 添加手势
    UITapGestureRecognizer *rightTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rightGes:)];
    [rightImageView addGestureRecognizer:rightTapGes];
    
}

#pragma mark - 返回上一页面
- (void)lastPage:(UITapGestureRecognizer *)tapGes
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 添加公告
- (void)rightGes:(UITapGestureRecognizer *)tapGes
{
    NSLog(@"删除公告");
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"" message:@"您确定要删除本公告么" preferredStyle:UIAlertControllerStyleAlert];
     // 修改标题颜色
    NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"提醒"];
    [title addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
    [title addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, 2)];
    [alertC setValue:title forKey:@"attributedTitle"];
    
    // 修改内容颜色
//    NSMutableAttributedString *content = [[NSMutableAttributedString alloc] initWithString:@"您确定要删除本公告么"];
//    [content addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, 10)];
//    [content addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 10)];
//    [alertC setValue:content forKey:@"attributedMessage"];
    
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"取消");
    }];
    // 修改取消按钮的颜色
   // [cancelAction setValue:[UIColor cyanColor] forKey:@"titleTextColor"];
    [alertC addAction:cancelAction];
    UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"删除");
    }];
    [alertC addAction:deleteAction];
    [self presentViewController:alertC animated:YES completion:nil];
}

#pragma mark - 建立数据源
- (void)addData
{
    // 需要取消换行
    NSString *tempString = self.myModel.content;
    NSString *endString = [tempString stringByReplacingOccurrencesOfString:@",\n" withString:@","];
    self.myModel.content = endString;
}

#pragma mark - 布局
- (void)addMySubview
{
    // 公告标题
    self.gongGaoTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15 * kAdjustParamater, 10 * kAdjustParamater, 300 * kAdjustParamater, 20 * kAdjustParamater)];
    self.gongGaoTitleLabel.text = self.myModel.gongGaoTitle;
 //   self.gongGaoTitleLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    self.gongGaoTitleLabel.textColor = [UIColor blackColor];
    self.gongGaoTitleLabel.textAlignment = NSTextAlignmentLeft;
    self.gongGaoTitleLabel.font = [UIFont systemFontOfSize:k18Big];
    [self.view addSubview:_gongGaoTitleLabel];
    
    // 发送者姓名
    self.senderNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(15 * kAdjustParamater,self.gongGaoTitleLabel.bottom + 10 * kAdjustParamater, 50 * kAdjustParamater, 15 * kAdjustParamater)];
    self.senderNameLabel.text = self.myModel.senderName;
  //  self.senderNameLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    self.senderNameLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    self.senderNameLabel.textAlignment = NSTextAlignmentLeft;
    self.senderNameLabel.font = [UIFont systemFontOfSize:k15Big];
    [self.view addSubview:_senderNameLabel];
    
    // 发送时间
    self.sendTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.senderNameLabel.right + 8 * kAdjustParamater, self.senderNameLabel.top, 200 * kAdjustParamater, 15 * kAdjustParamater)];
    self.sendTimeLabel.text = self.myModel.time;
 //   self.sendTimeLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    self.sendTimeLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    self.sendTimeLabel.textAlignment = NSTextAlignmentLeft;
    self.sendTimeLabel.font = [UIFont systemFontOfSize:k15Big];
    [self.view addSubview:_sendTimeLabel];
    
    // 下方灰色条
    UILabel *grayLine1 = [[UILabel alloc] initWithFrame:CGRectMake(0, self.senderNameLabel.bottom + 10 * kAdjustParamater, kScreenWidth, 2 * kAdjustParamater)];
    grayLine1.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [self.view addSubview:grayLine1];
    
    // 正文
    self.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(15 * kAdjustParamater, grayLine1.bottom  + 10 * kAdjustParamater, 345 * kAdjustParamater, 50 * kAdjustParamater)];
  //  self.contentLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    self.contentLabel.text = self.myModel.content;
    self.contentLabel.textAlignment = NSTextAlignmentLeft;
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.textColor = [UIColor blackColor];
    self.contentLabel.font = [UIFont systemFontOfSize:k15Big];
    [self.view addSubview:_contentLabel];
    // 自适应高度
    CGRect newFrame = self.contentLabel.frame;
    newFrame.size.height = [[NetStatusHandle shareHandle] adjustHeight:self.contentLabel.text size:15 width:345];
    self.contentLabel.frame = newFrame;
    
    // 底部编辑公告背景
    self.bottomWhiteView = [[UIView alloc] initWithFrame:CGRectMake(0, (kScreenHeight - 70 - 64) * kAdjustParamater, kScreenWidth, 70 * kAdjustParamater)];
    self.bottomWhiteView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_bottomWhiteView];
    
    // 灰色条
    UILabel *grayLine2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 2 * kAdjustParamater)];
    grayLine2.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [self.bottomWhiteView addSubview:grayLine2];
    
    // 编辑图片
    UIImageView *editImageV = [[UIImageView alloc] initWithFrame:CGRectMake(125 * kAdjustParamater, 21 * kAdjustParamater, 25 * kAdjustParamater, 25 * kAdjustParamater)];
  //  editImageV.backgroundColor = [UIColor redColor];
    editImageV.image = [UIImage imageNamed:@"bianjiaGongGao-tianjiagongao"];
    [self.bottomWhiteView addSubview:editImageV];
    
    // 编辑公告按钮
    self.editGongGaoButton = [UIButton buttonWithType:UIButtonTypeSystem];
  //  self.editGongGaoButton.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.1];
    self.editGongGaoButton.frame = CGRectMake(editImageV.right + 20 * kAdjustParamater, editImageV.top, 100, 25 * kAdjustParamater);
    [self.editGongGaoButton setTitle:@"编辑公告" forState:UIControlStateNormal];
    [self.editGongGaoButton setTitleColor:kCyanColor forState:UIControlStateNormal];
    [self.editGongGaoButton addTarget:self action:@selector(edit:) forControlEvents:UIControlEventTouchUpInside];
    self.editGongGaoButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    // 可以设置字体距离左边的距离
   // self.editGongGaoButton.contentEdgeInsets
    self.editGongGaoButton.titleLabel.font = [UIFont systemFontOfSize:22 * kAdjustParamater];
    [self.bottomWhiteView addSubview:_editGongGaoButton];
    
}

#pragma mark - 编辑公告
- (void)edit:(UIButton *)button
{
    NSLog(@"编辑公告");
    EditGongGaoViewController *editGongGaoVC = [[EditGongGaoViewController alloc] init];
    editGongGaoVC.editGongGaoModel = self.myModel;
    [self.navigationController pushViewController:editGongGaoVC animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
